/*
 * test_dealplayer.cpp
 *
 *  Created on: Apr 15, 2014
 *      Author: killerg
 */

#include "share/db_mgr.h"
#include "share/util.h"

static void deal_player(Json::Value &newvalue) {
	int i=0;
	for (auto itr = newvalue["syncatt"]["n4"].begin();
		itr != newvalue["syncatt"]["n4"].end();
		++itr, ++i)
	{
		switch (i) {
		case 18:
            if (*itr <= 0)
                (*itr) = 10;
			break;
		}
	}
//	Json::Value &spritesjson = newvalue["sprites"];
//	for (auto itr = spritesjson.begin(); itr != spritesjson.end(); ++itr) {
//		for (auto skill_itr = (*itr)["skill_level"].begin();
//			skill_itr != (*itr)["skill_level"].end();
//			++skill_itr)
//		{
//			if ((*skill_itr).asInt() > 30) {
//				(*skill_itr) = 30;
//			}
//		}
//	}
//
//	Json::Value &petsjson = newvalue["pets"];
//	for (auto itr = petsjson.begin(); itr != petsjson.end(); ++itr) {
//		int i=0;
//		for (auto itr2 = (*itr)["syncatt"]["n4"].begin();
//			itr2 != (*itr)["syncatt"]["n4"].end();
//			++itr2, ++i)
//		{
//			switch (i) {
//			case 4:
//			case 12:
//			case 13:
//			case 14:
//			case 15:
//			case 38:
//				(*itr2) = 0;
//				break;
//			}
//		}
//	}
	newvalue["_dealt"] = 3;
}


bool test_dealplayer() {
	db_mgr_t db;

	if (!db.init("localhost", "", "", "xone_gamedata")) {
		printf("init error\n");
		return false;
	}
	int total = 0;
	int cur = 0;

	do {
		Json::Value view_data;
		std::string path = fmt_cstr(
				"/_design/upgrade/_view/players"
				"?connection_timeout=60000"
//ZZ				"&limit=2000"
                "&key=2"
				"&stale=false"
				"&skip=%d",
				cur);
		bool res = db.view(
				path.c_str(),
				[&view_data](db_error_t err, const Json::Value &json){
					assert(err == DBE_Success);
					view_data = json;
				});
		assert(res);
		db.wait();

		total = view_data["total_rows"].asInt();
		const Json::Value &rows_data = view_data["rows"];
		int rows = rows_data.size();
		for (int i=0; i<rows; ++i, ++cur) {
			const Json::Value &row_data = rows_data.get(i, Json::nullValue);
			if (row_data.isNull())
				continue;

			Json::Value player_data;
			bool res = db.get(
					row_data["id"].asCString(),
					[&player_data](db_error_t err, const Json::Value &json){
						assert(err == DBE_Success);
						player_data = json;
					});
			assert(res);
			db.wait();

			std::string file_path = fmt_cstr(
					"%s_old",
					row_data["id"].asCString()
					);
			for (int i=0; i<file_path.size(); ++i) {
				if (file_path[i] == ':')
					file_path[i] = '_';
			}
			assert(save_file(file_path.c_str(), player_data.toStyledString().c_str()));

			deal_player(player_data);

			res = db.set(
					row_data["id"].asCString(),
					player_data,
					[](db_error_t err){
						assert(err == DBE_Success);
					});
			assert(res);
			db.wait();
			file_path = fmt_cstr(
					"%s_new",
					row_data["id"].asCString()
					);
			for (int i=0; i<file_path.size(); ++i) {
				if (file_path[i] == ':')
					file_path[i] = '_';
			}

			assert(save_file(file_path.c_str(), player_data.toStyledString().c_str()));

			printf(".");
		}
		printf("\n%d \n", cur);
	} while (cur < total);

	db.destroy();
	printf("done\n");

	return true;
}
